﻿using AuroraPlatform.Tools;
using AuroraPlatform.View.Level5;
using AuroraPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AuroraPlatform.View
{
    public partial class CartWindow : ChildWindow
    {
        private ColumnDefinition _firstColumn = null;

        public CartWindow()
        {
            InitializeComponent();

            ExportExcelBtn.Click += ExportExcelBtn_Click;
            ExportPdfBtn.Click += ExportPdfBtn_Click;
            CancelButton.Click += CancelButton_Click;

            for (int i = 0; i < 2; i++)
            {
                TitleGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(23) });
            }

            _firstColumn = new ColumnDefinition() { Width = new GridLength(70) };
            TitleGrid.ColumnDefinitions.Add(_firstColumn);
            for (int i = 0; i < 10; i++)
            {
                TitleGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(i == 4 ? 220 : 75) });
            }


            TitleGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(205) });

            AddTable(CreateLighting(), 0);
            AddTable(CreateSource(), 6);
            AddTable(CreateOptics(), 10);
            AddTable(CreateBusiness(), 11);

            DataGrid.ItemsSource = BusinessContext.Singleton.Items;
        }

        private async void ExportPdfBtn_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            int index = 0;
            foreach (LightingListModel model in BusinessContext.Singleton.Items)
            {
                index++;
                sb.AppendFormat("{0},", model.ID);
            }

            if (index == 0)
            {
                MessageHelper.ShowMessage("灯具车中未发现任何数据。");
                return;
            }

            BatchDelete.Builder builder = new BatchDelete.Builder();
            builder.SetId(sb.Remove(sb.Length - 1, 1).ToString());

            busy.BusyContent = "正在导出数据，请稍等......";
            busy.IsBusy = true;
            WebServiceProxy proxy = new WebServiceProxy();
            string url = await proxy.CustomExportToPDF(builder.Build().ToByteArray());
            busy.IsBusy = false;
            if (!string.IsNullOrEmpty(url))
                HtmlPage.Window.Navigate(new Uri(url));
            else
                MessageHelper.ShowError("导出数据出错。");
        }

        private async void ExportExcelBtn_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            int index = 0;
            foreach (LightingListModel model in BusinessContext.Singleton.Items)
            {
                index++;
                sb.AppendFormat("{0},", model.ID);
            }

            if (index == 0)
            {
                MessageHelper.ShowMessage("灯具车中未发现任何数据。");
                return;
            }

            BatchDelete.Builder builder = new BatchDelete.Builder();
            builder.SetId(sb.Remove(sb.Length - 1, 1).ToString());

            busy.BusyContent = "正在导出数据，请稍等......";
            busy.IsBusy = true;
            WebServiceProxy proxy = new WebServiceProxy();
            string url = await proxy.CustomExportToExcel(builder.Build().ToByteArray());
            busy.IsBusy = false;
            if (!string.IsNullOrEmpty(url))
                HtmlPage.Window.Navigate(new Uri(url));
            else
                MessageHelper.ShowError("导出数据出错。");
        }


        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }


        private void AddTable(LightingTable table, int columnIndex)
        {
            SolidColorBrush brush = new SolidColorBrush(table.Backgroud);

            Grid tableTitle = CreateTitleCell(table.Name, 0, columnIndex, 14);
            tableTitle.Background = brush;
            TitleGrid.Children.Add(tableTitle);
            Grid.SetColumn(tableTitle, columnIndex);
            Grid.SetColumnSpan(tableTitle, table.Columns.Length);

            int startColumnIndex = 0;
            foreach (ColumnProperty column in table.Columns)
            {
                Grid columnTitle = CreateTitleCell(column.Name, 1, startColumnIndex + columnIndex);
                columnTitle.Background = brush;
                TitleGrid.Children.Add(columnTitle);
                Grid.SetRow(columnTitle, 1);
                Grid.SetColumn(columnTitle, startColumnIndex + columnIndex);

                startColumnIndex++;
            }
        }

        private Grid CreateTitleCell(string text, int rowIndex, int columIndex, double fontsize = 13)
        {
            Grid grid = new Grid();

            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.White);

            if (rowIndex == 0)
            {
                if (columIndex > 0)
                {
                    border.BorderThickness = new Thickness(1, 0, 0, 0);
                }
                else
                {
                    border.BorderThickness = new Thickness(0);
                }
            }
            else
            {
                if (columIndex == 0)
                {
                    border.BorderThickness = new Thickness(0, 1, 0, 0);
                }
                else
                {
                    border.BorderThickness = new Thickness(1, 1, 0, 0);
                }
            }

            grid.Children.Add(border);

            TextBlock tb = new TextBlock();
            tb.Text = text;
            tb.FontSize = fontsize;
            tb.HorizontalAlignment = HorizontalAlignment.Center;
            tb.VerticalAlignment = VerticalAlignment.Center;

            grid.Children.Add(tb);
            return grid;
        }

        public LightingTable CreateLighting()
        {
            return new LightingTable()
            {
                Name = "产品描述",
                Columns = new ColumnProperty[]
                {
                    new ColumnProperty { Name = "产品图片" },
                    new ColumnProperty { Name = "产品系列" },
                    new ColumnProperty { Name = "产品分类" },
                    new ColumnProperty { Name = "规格型号" },
                    new ColumnProperty { Name = "产品品牌" },
                    new ColumnProperty { Name = "外形尺寸" },
                },
                Backgroud = Color.FromArgb(255, 234, 234, 234),
            };
        }

        public LightingTable CreateSource()
        {
            return new LightingTable()
            {
                Name = "光源描述",
                Columns = new ColumnProperty[]
                {
                    new ColumnProperty { Name = "光源种类" },
                    new ColumnProperty { Name = "光源功率" },
                    new ColumnProperty { Name = "光学色温" },
                    new ColumnProperty { Name = "显色性" },
                },
                Backgroud = Color.FromArgb(255, 204, 204, 204),
            };
        }

        public LightingTable CreateOptics()
        {
            return new LightingTable()
            {
                Name = "光学参数",
                Columns = new ColumnProperty[]
                {
                    new ColumnProperty { Name = "光束角" },
                },
                Backgroud = Color.FromArgb(255, 234, 234, 234)

            };
        }

        public LightingTable CreateBusiness()
        {
            return new LightingTable()
            {
                Name = "商务参数",
                Columns = new ColumnProperty[]
                {
                    new ColumnProperty { Name = "市场参考价"},
                },
                Backgroud = Color.FromArgb(255, 204, 204, 204)
            };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            LightingListModel item = btn.DataContext as LightingListModel;
            BusinessContext.Singleton.Items.Remove(item);
            BusinessContext.Singleton.CartCount = BusinessContext.Singleton.Items.Count;
        }
    }
}

